<?php
defined('BASEPATH') or die('forbidden');
class Bag_Controller Extends Controller
{
	private $bagtypelist;	//背包类型
	private $objectlist;	//物品名字
	private $dbConnt;	//数据库连接
	private $playerlist;  //玩家名字
    private $m;	//数据库连接
    private $server_info;
	
	function _init()
	{
		check_level(50,segment(0),segment(1));
		set_time_limit(0);
		$this->bagtypelist = get_type_package();
		$this->objectlist = get_type_object_db();
		//$this->db->select_db();
		
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        //query server mapping
        //$selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' AND areanum={$areanum} ";
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        //var_dump($this->server_info);
        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
	}

	function index()
	{
		$this->view->display("dbbag/bag/index");
	}
	function flexigrid()
	{
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerBag;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);

        foreach ($document as $key=>$value){
            foreach ($value['herobag']['heros'] as $k=>$v){
                $v['_id']=$value['_id'];
                $v['type']='hero';
                $num=$key.'-'.$k;
                $data[$num]=array('id' => $value['_id'], 'cell' => $v);
            }

        }
//		$sql = "SELECT COUNT(*) AS num FROM player_package WHERE {$where} {$exwhere}";
//		//echo " sql2:$sql ";
//		$one = $this->dbConnt->one($sql);
//		$total = $one['num'];
//
//		$sql = "SELECT id, name from player";
//		$this->dbConnt->query($sql);
//		while($n = $this->dbConnt->get_array('default'))
//		{
//			$this->playerlist[$n['id']] = trim($n['name']);
//		}
//		//print_r($this->playerlist);
//		//exit(0);
//
//		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
//
//		$sql = "SELECT {$colsinfo} FROM  player_package WHERE {$where} {$exwhere} {$orderby} LIMIT {$offset},{$rp}";
//		$this->dbConnt->query($sql);
//		//$id = 0;
//		while ($n = $this->dbConnt->get_array('default'))
//		{
//			//print_r($n);
//			//时间需要由数字转成字符串
//			$n['indate']=get_datetime($n['indate']);
//			//位置需要转化下
//			$n['packid']=$this->bagtypelist[$n['packid']]['chinaname']."-(".$n['packid'].")";
//			$n['typeid']=$this->objectlist[$n['typeid']]."-(".$n['typeid'].")";
//			$n['roleid']=$n['roleid']."-(".$this->playerlist[$n['roleid']].")";
//
//			$rows[] = array('id' => $n['gmid'], 'cell' => $n);
//			//$id = $id+1;
//		}
		//print_r($rows);
		//print_r($this->playerlist);
		//exit(0);
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $data
		);

		echo json_encode($result);
	}
	
	function del($id)
	{
		/*
		$area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
		$url=$area_host.$id;
		$strResult=vpost($url);
		$result=1;
		if ($strResult!=0||$strResult==FALSE)
		{
			$result=0;
		}
		json_output($result, translate('C_TIPS_ERROR_TEXT'));
		*/
	}
}
?>